import Rise from '../../packages/';
import axios from "@/plugins/axios";

//字典值配置
const getDic = val => {
    return axios({
        //这里为自己的字典值接口
        url: "/api/blade-system/dict-biz/dictionary",
        method: "get",
        params: {
            code: val
        }
    }).then(response => {
        //返回出来的数据处理，最后retuen数组
        return response.data.data;
    });
};

//下载配置
const downloadFn = data => {
    let attachId = "";
    if (typeof data == "string") {
        attachId = data;
    } else {
        attachId = data.attachId;
    }

    axios({
            url: "api/blade-resource/oss/endpoint/downloadFile",
            params: {
                attachId
            },
            method: "get",
            responseType: "arraybuffer"
        })
        .then(res => {
            if (res) {
                let headers = res.headers;
                let content = headers["content-disposition"];
                //下载后文件名
                let filename = decodeURI(content.split(";")[1].split("filename=")[1]);
                let blob = new Blob([res.data], {
                    type: res.headers["content-type"]
                });
                let downloadElement = document.createElement("a");
                // 创建下载链接
                let href = URL.createObjectURL(blob);
                downloadElement.href = href;
                downloadElement.download = filename;
                document.body.appendChild(downloadElement);
                // 点击下载
                downloadElement.click();
                // 下载移除元素
                document.body.removeChild(downloadElement);
                // 释放掉blob对象
                URL.revokeObjectURL(href);
            } else {
                this.$msgUtil.errorTip("获取下载地址失败");
            }
        })
        .catch(() => {
            this.$msgUtil.errorTip("下载失败");
        });
};

//项目自有组件配置
const otherWidet = [{
    "title": "组件",
    "list": [{
        "name": "自由组件",
        "type": "form-title",
        "title": "标题",
        "grade": "brick",
        "icon": "el-icon-star-off",
        "textAlign": "center",
        "formItem": {
            "span": 24,
            "noLabel": true,
            "labelWidth": "0px"
        }
    }]
}]

const config = {
    getDic,
    downloadFn,
    axios,
    otherWidet
}

const _Rise = Rise.create(config);

export default _Rise